/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-11-25 13:52:43
 ******************************************************************************/
package cn.com.xy.sms.util;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import cn.com.xy.sms.sdk.dex.DexUtil;

public class ParseNotificationManager {

    public static Map<String, Object> parseNotificationMsgAndPopupWindow(Context ctx, String msgId, String phoneNumber, String smsCenterNum, String smsContent, long smsReceiveTime, HashMap<String, String> extend) throws Exception {
        /* RM-903 wangxingjian 20161104 start */
        if(extend == null){
            extend = new HashMap<String, String>();
        }

        if(!extend.containsKey("from")){
            extend.put("from", "1");
        }
        /* RM-903 wangxingjian 20161104 end */
        Map<String, Object> res = parseSmsToBubbleResultMap(ctx, msgId, phoneNumber, smsCenterNum, smsContent, smsReceiveTime, extend);
        if (res != null) {
            // parse bubble 流程在parseSmsToBubbleResultMap 中已处理。在调用弹窗环节无须再处理。
            ParseManager.parseMsgToPopupWindow(ctx, phoneNumber, smsCenterNum, smsContent, res, false, extend);
            return DexUtil.handerNotificationValueMap(res);
        }
        return null;
    }

    public static Map<String, Object> parseNotificationMsg(Context ctx, String msgId, String phoneNumber, String smsCenterNum, String smsContent, long smsReceiveTime, HashMap<String, String> extend) throws Exception {
        Map<String, Object> res = parseSmsToBubbleResultMap(ctx, msgId, phoneNumber, smsCenterNum, smsContent, smsReceiveTime, extend);
        if (res != null) {
            ParseSmsToBubbleUtil.backGroundHandleMapByType(extend, res);
            return DexUtil.handerNotificationValueMap(res);
        }
        return null;
    }

    private static Map<String, Object> parseSmsToBubbleResultMap(Context ctx, String msgId, String phoneNumber, String smsCenterNum, String smsContent, long smsReceiveTime, HashMap<String, String> extend) throws Exception {
        if (ctx == null) {
            throw new Exception(" Context is null.");
        }

        if (phoneNumber == null) {
            throw new Exception(" phoneNumber is null.");
        }
        if (smsContent == null) {
            throw new Exception(" smsContent is null.");
        }
        return ParseSmsToBubbleUtil.parseSmsToBubbleResultMap(msgId, phoneNumber, smsContent, smsCenterNum, smsReceiveTime, 4, true, true, extend);
    }

    public static Map<String, Object> parseNotificationMsg(Context ctx, long msgId, String phoneNumber, String smsCenterNum, String smsContent, Map<String, String> extend) throws Exception {
        if (ctx == null) {
            throw new Exception(" Context is null.");
        }


        if (phoneNumber == null) {
            throw new Exception(" phoneNumber is null.");
        }
        if (smsContent == null) {
            throw new Exception(" smsContent is null.");
        }
        /* SDK-402 huangzhiqiang 20160420 start */
        extend = ParseManager.putValueToMap(extend, "msgId", String.valueOf(msgId));
        /* SDK-402 huangzhiqiang 20160420 end */
        Map<String, Object> res = ParseManager.parseMsg(ctx, phoneNumber, smsCenterNum, smsContent, 0l, extend);
        Map<String, Object> map = null;
        if (res != null && ParseBubbleManager.getParseStatu(res) != -1) {
            map = DexUtil.handerNotificationValueMap(res);
            if (map == null) {
                map = new HashMap<String, Object>();
                map.put("Result", false);
                return map;
            }
            map.put("Result", true);
            return map;
        } else {
            map = new HashMap<String, Object>();
            map.put("Result", false);
            return map;
        }
    }
}
